package assignment1;

import java.util.ArrayList;
import java.util.Random;

public class GridView {

  
    private int Grid_Start = 0;
    private int Boundary_One = 10;
    private int Boundary_Two = 10;
    private static final int Initial_Hor_Loca = 0;
    private static final int Boundary_Light_One = 3;
    private static final int Boundary_Light_Two = 6;
    private static final int Boundary_Light_Three = 9;
    private static final int Light_Loca= 9;
    private static final int Light_Loca_Vert = 14;
    private static final int Light_Loca_Hort = 9;
    private ArrayList<Car> carsArrVert;
    private ArrayList<Car> carsArrHort;


    char[][] cell;
    ProjectModel pmodel;
    int gSize;
    ArrayList<Car> carArray;

    public GridView(){
}
    public GridView(ProjectModel pmodel){
        this.pmodel = pmodel;
        cell = new char[pmodel.getGridSize()][pmodel.getGridSize()];
        gSize = pmodel.getGridSize();
        carArray = new ArrayList<Car>();
        carsArrVert = new ArrayList<Car>();
        carsArrHort = new ArrayList<Car>();

    }

    public void initialGrid(char cha){
        for (int i = 0; i < gSize; i++) {
            for (int j = 0; j < gSize; j++) {
                cell [i][j] = cha;

            }
            buildHortLine();
            buildVertLine();
            setCarsGView();
        }

    }
    public void show (){
        hortLight();
        vertLight();
        setCarsGView();
        showGrid();
    }

    public void showGrid(){
            for (int i = 0; i < gSize; i++) {
                for (int j = 0; j < cell.length; j++) {
                    System.out.print(cell[i][j]);

                }
                System.out.println();
            }

    }

public void buildHortLine(){
    int mid = gSize/2;
    int hortLane = pmodel.getHortLanes();

    for (int i = 0; i < gSize; i++) {
        for (int j = 0; j < hortLane; j++) {
            cell[mid+j][i] = '-';

        }
    }
}

public void buildVertLine(){
    int mid = gSize/2;
    int vertLane = pmodel.getVertLanes();

    for (int i = 0; i < gSize; i++) {
        for (int j = 0; j < vertLane; j++) {
          cell [i][mid+j] = '|';

        }
    }
}
public void vertLight(){
    if(pmodel.getTrafVertLight()== true){
        cell[Light_Loca][Light_Loca_Vert] = 'G';
    }else if (pmodel.getTrafVertLight() == false){
        cell[Light_Loca][Light_Loca_Vert] = 'R';
    }
}


public void hortLight(){
    if(pmodel.getTrafHortLight() == true){
        cell[Light_Loca][Light_Loca_Hort] = 'G';
    }else if (pmodel.getTrafHortLight() == false){
        cell[Light_Loca][Light_Loca_Hort] = 'R';
    }
}


private void setCarsGView(){
    for (int i = 0; i < carsArrVert.size(); i++) {
        Car cars= carsArrVert.get(i);
        int x = cars.getXPosition();
        int y = cars.getYPosition();
        cell[x][y] = 't';
    }
    for (int i = 0; i < carsArrHort.size(); i++) {
        Car cars = carsArrHort.get(i);
        int x = cars.getXPosition();
        int y = cars.getYPosition();
        cell[x][y] = 'c';

    }
}


 private void addCars(){
        addCarsHort();
        addCarsVert();
        show();

    }


    private void addCarsVert(){
        for (int i = 0; i < pmodel.getVertLanes(); i++) {
            Random random = new Random();
            float randomNum = random.nextFloat();
            if (randomNum < pmodel.getVertProbl()){
               carsArrVert.add(new Car (Initial_Hor_Loca, Boundary_Two + i));
            }else if (randomNum > pmodel.getVertLanes()){

            } moveVertCars();
        }
    }

    private void addCarsHort(){
        for (int i = 0; i < pmodel.getHortLanes() ; i++) {
           Random random = new Random();
           float randomNum = random.nextFloat();
           if(randomNum < pmodel.getHortProbl()){
            carsArrHort.add(new Car (Boundary_One + i, Initial_Hor_Loca));

           }else if(randomNum > pmodel.getHortProbl()){
           }
           moveHortCars();
        }
    }
    public void moveCars(int numSimul){
        for (Grid_Start= 1;  Grid_Start<= numSimul; Grid_Start++) {
            if(Grid_Start > numSimul){
                Grid_Start = 1;
            }
            if(Grid_Start <= numSimul){
                    pmodel.setTrafHortLight(false);
                    pmodel.setTrafVertLight(true);
                if (Grid_Start <= Boundary_Light_Three) {
                            pmodel.setTrafHortLight(true);
                            pmodel.setTrafVertLight(false);
                    if (Grid_Start <= Boundary_Light_Two) {
                            pmodel.setTrafHortLight(false);
                            pmodel.setTrafVertLight(true);
                        if (Grid_Start <= Boundary_Light_One) {
                            pmodel.setTrafHortLight(true);
                            pmodel.setTrafVertLight(false);

                        }
                    }
                }
                    System.out.println("");
            }

            addCars();
            System.out.println("");
        }
        
}
    

    private void moveVertCars(){
        for (int ecar = 0; ecar < carsArrVert.size(); ecar++) {
            Car car = carsArrVert.get(ecar);
            int x = car.getXPosition();
            if(x != Light_Loca){
                car.setXPosition(x += 1);
            }
            if (x == Light_Loca){
                if(pmodel.getTrafVertLight()== true){
                    car.setXPosition(x +=1);
                }
            }
            if(x >= pmodel.getGridSize()){
                carsArrVert.remove(car);
            }
        }
   }
   private void moveHortCars(){
       for(int ecar = 0; ecar < carsArrHort.size(); ecar++){
           Car car = carsArrHort.get(ecar);
           int y = car.getYPosition();
           if (y != Light_Loca){
               car.setYPosition(y += 1);
           }
           if (y == Light_Loca){
               if(pmodel.getTrafHortLight() == true){
                   car.setYPosition(y += 1);
               }
           }
           if (y >= pmodel.getGridSize()){
               carsArrHort.remove(car);
           }
       }
   }

}

